﻿#include "chatroom.h"

#include <QDebug>

#include "member.h"

ChatRoom::ChatRoom() : AbstractChatRoom()
{

}

void ChatRoom::registerMember(int id, Member *member)
{
    MemberMap::const_iterator it = m_memberMap.find(id);
    if (it == m_memberMap.end()) {
        m_memberMap.insert(id, member);
        member->setChartRoom(this);
    }
}

void ChatRoom::sendImage(int fromID, int toID, QString imageName)
{
    MemberMap::const_iterator toIT = m_memberMap.find(toID);
    if (toIT == m_memberMap.end()) {
        return;
    }
    Member* toMember = toIT.value();

    MemberMap::const_iterator fromIT = m_memberMap.find(fromID);
    Member* fromMember = fromIT.value();

    if (fromMember->level() == 0) {
        qDebug() << "current member: " << fromID << "has no right to send Image";
    } else {
        toMember->receiveImage(toID, imageName);
    }
}

void ChatRoom::sendText(int fromID, int toID, QString textInfo)
{
    MemberMap::const_iterator it = m_memberMap.find(toID);
    if (it == m_memberMap.end()) {
        return;
    }

    Member* member = it.value();
    member->receiveImage(toID, textInfo);
}
